﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Windows.Forms;
using QuanLyKhachSan.DataAccess;
using QuanLyKhachSan.BusinessObject;
namespace QuanLyKhachSan.BusinessLogic
{
    class LoaiPhongBL
    {
        ConnectData connData = new ConnectData();
        public DataTable LayDSLoaiPhong() {
            string sql = "LayDSLoaiPhong";
            return connData.GetDataTable(sql);
        }

        //Tim kiem
        public DataTable TimKiemLoaiPhong(string timKiem)
        {
            string sql = "select * from LoaiPhong where TenLoaiPhong like '%" + timKiem + "%' or KieuPhong like '%" + timKiem + "%' or MaLoaiPhong like '%" + timKiem + "%' or ChatLuong like '%" + timKiem + "%' or DonGia like '%" + timKiem + "%'";
            return connData.GetDataTable(sql);
        }
        //Lấy tien
        public static int LayTien(string maLoaiPhong) {
            int donGia = 0;
            ConnectData connData1 = new ConnectData();
            SqlConnection conn = connData1.Connect1();
            conn.Open();
            SqlCommand objCmd = new SqlCommand("select DonGia from LoaiPhong where MaLoaiPhong='" + maLoaiPhong + "'", conn);
            SqlDataReader dt = objCmd.ExecuteReader();
            while (dt.Read())
            {
                donGia = Int32.Parse(dt.GetValue(0).ToString());
            }
            conn.Close();
            return donGia;
        }
        

        //kiểm tra textbox trước khi ấn lưu
        public bool KiemTraTruocKhiAnLuu(LoaiPhongBO lp) {
            
            return true;
        }

        //thêm mới loại phòng
        public void ThemLP(LoaiPhongBO lp)
        {

            //string sql = string.Format("INSERT INTO LoaiPhong (MaLoaiPhong, TenLoaiPhong, KieuPhong, ChatLuong, DonGia)" + "VALUES ('{0}','{1}','{2}','{3}','{4}')", lp.MaLoaiPhong, lp.TenLoaiPhong, lp.KieuPhong, lp.ChatLuong, lp.DonGia);
            //if (connData.ExeCuteQuery(sql))
            //{
            //    MessageBox.Show("Thêm mới chức vụ thành công!", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);

            //}
            SqlConnection conn = connData.Connect1();
            conn.Open();
            string sql = "ThemMoiLoaiPhong";
            SqlCommand objCmd = new SqlCommand(sql, conn);
            objCmd.CommandType = CommandType.StoredProcedure;
            objCmd.Parameters.Add("@MaLoaiPhong", SqlDbType.NVarChar, 30);
            objCmd.Parameters.Add("@TenLoaiPhong", SqlDbType.NVarChar, 30);
            objCmd.Parameters.Add("@KieuPhong", SqlDbType.NVarChar, 30);
            objCmd.Parameters.Add("@ChatLuong", SqlDbType.NVarChar, 30);
            objCmd.Parameters.Add("@DonGia", SqlDbType.Int);

            objCmd.Parameters["@MaLoaiPhong"].Value = lp.MaLoaiPhong;
            objCmd.Parameters["@TenLoaiPhong"].Value = lp.TenLoaiPhong;
            objCmd.Parameters["@KieuPhong"].Value = lp.KieuPhong;
            objCmd.Parameters["@ChatLuong"].Value = lp.ChatLuong;
            objCmd.Parameters["@DonGia"].Value = lp.DonGia;
            objCmd.ExecuteNonQuery();
            conn.Close();
        }

        //
        public string NextID()
        {
            return Utilities.NextID(connData.GetLastID("LoaiPhong", "MaLoaiPhong"), "LP");
        }

        public bool CheckExists(string malp)
        {
            if (connData.CheckExistValue("LoaiPhong", "MaLoaiPhong", malp))
            {
                return true;
            } return false;
        }

        //sửa
        public void SuaLp(LoaiPhongBO lp)
        {
            
                //string sql = string.Format("UPDATE LoaiPhong SET TenLoaiPhong = '{0}', KieuPhong = '{1}', ChatLuong = '{2}', DonGia = '{3}' WHERE MaLoaiPhong = '{4}' ", lp.TenLoaiPhong, lp.KieuPhong, lp.ChatLuong, lp.DonGia, lp.MaLoaiPhong);
                //if (connData.ExeCuteQuery(sql))
                //{
                //    MessageBox.Show("Sửa Loại Phòng thành công!", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    
                //}
            SqlConnection conn = connData.Connect1();
            conn.Open();
            string sql = "SuaLoaiPhong";
            SqlCommand objCmd = new SqlCommand(sql, conn);
            objCmd.CommandType = CommandType.StoredProcedure;
            objCmd.Parameters.Add("@MaLoaiPhong", SqlDbType.NVarChar, 30);
            objCmd.Parameters.Add("@TenLoaiPhong", SqlDbType.NVarChar, 30);
            objCmd.Parameters.Add("@KieuPhong", SqlDbType.NVarChar, 30);
            objCmd.Parameters.Add("@ChatLuong", SqlDbType.NVarChar, 30);
            objCmd.Parameters.Add("@DonGia", SqlDbType.Int);

            objCmd.Parameters["@MaLoaiPhong"].Value = lp.MaLoaiPhong;
            objCmd.Parameters["@TenLoaiPhong"].Value = lp.TenLoaiPhong;
            objCmd.Parameters["@KieuPhong"].Value = lp.KieuPhong;
            objCmd.Parameters["@ChatLuong"].Value = lp.ChatLuong;
            objCmd.Parameters["@DonGia"].Value = lp.DonGia;
            objCmd.ExecuteNonQuery();
            conn.Close();
        }


        //xóa
        public void Delete(string lp)
        {
            string sql = "Delete from LoaiPhong where MaLoaiPhong='" + lp + "'";
            connData.ExeCuteQuery(sql);
        }
    }
}
